package com.sanid.wqb;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author schroetelm
 *
 */
public class QuestionRepository {
	
	private static QuestionRepository instance;
	
	public static QuestionRepository getInstance() {
		if(instance == null) {
			instance = new QuestionRepository();
			try {
				InputStream is = instance.getClass().getResourceAsStream("/questions.txt");
				InputStreamReader isr = new InputStreamReader(is);
				BufferedReader br = new BufferedReader(isr);
				String line = null;
				while((line = br.readLine()) != null) {
					String[] split = line.split("//");
					if(split.length == 2) {
						// Valid line
						Question question = new Question();
						question.setQuestion(split[1]);
						
						// Split first part of string
						split = split[0].split("/");
						if(split.length >= 2) {
							// Question seems valid
							question.setCategory(split[1]);
							question.setAnswer(split[2]);
							for(int i=3; i<split.length; i++) {
								question.addAlternateAnswer(split[i]);
							}
							
							// Add question to repository
							instance.questions.add(question);
						}
					}
				}
			} catch(Exception e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
		return instance;
	}
	
	private List<Question> questions = new ArrayList<Question>();

	/**
	 * @return the questions
	 */
	public List<Question> getQuestions() {
		return questions;
	}
	
}
